package com.example.yoho.mvp.presenter;

import android.util.Log;

import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.RegisterContract;
import com.example.yoho.mvp.model.entity.RegisterEntity;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterPresenter extends BasePresenter<RegisterContract.Model,RegisterContract.View> {

    String name;
    String pwd;
    @Inject
    public RegisterPresenter() {
    }

    public void RegisterAll(String name,String pwd){
        this.name=name;
        this.pwd=pwd;

        JSONObject registerJob = new JSONObject();
        try {
            registerJob.put("username",name);
            registerJob.put("password",pwd);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("333333", "RegisterAll: "+registerJob.toString());

        mModel.registerModel(registerJob.toString())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<RegisterEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                        mView.showDialog();
                    }

                    @Override
                    public void onNext(RegisterEntity registerEntity) {
                            mView.showRegister(registerEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showToast("请求失败"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.hideDialog();
                    }
                });
    }
}
